<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>分类参数</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 分类参数卡片 -->

    <el-card class="box-card">
      <el-alert
        title="注意:只能选择第三级的元素"
        type="warning"
        center
        show-icon
        :closable="false"
      >
      </el-alert>
      <el-row :gutter="20" class="elrow">
        <el-col :span="3" class="casSpan">请选择商品分类</el-col>
        <el-col :span="10"
          ><el-cascader
            v-model="CateValue"
            :options="CateList"
            :props="{
              expandTrigger: 'hover',
              label: 'cat_name',
              value: 'cat_id',
            }"
            @change="handleChange"
          ></el-cascader
        ></el-col>
      </el-row>

      <!-- 动态参数加载框 -->
      <el-dialog title="添加动态参数" :visible.sync="ManyVisible" width="30%">
        <el-form label-position="left" label-width="80px" :model="manyForm">
          <el-form-item label="参数名称">
            <el-input v-model="manyForm.attr_name"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="ManyVisible = false">取 消</el-button>
          <el-button type="primary" @click="addManyInfo">确 定</el-button>
        </span>
      </el-dialog>

      <!-- 修改动态参数 -->
      <el-dialog
        title="修改动态参数"
        :visible.sync="editManyVisible"
        width="30%"
      >
        <el-form
          label-position="right"
          label-width="80px"
          :model="editManyForm"
        >
          <el-form-item label="新参数名">
            <el-input v-model="editManyForm.attr_name"></el-input>
          </el-form-item>
          <!-- <el-form-item label="参数值">
            <el-input v-model="editManyForm.attr_vals"></el-input>
          </el-form-item> -->
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="editManyVisible = false">取 消</el-button>
          <el-button type="primary" @click="editManyInfo">确 定</el-button>
        </span>
      </el-dialog>

      <!-- 修改静态属性 -->

      <el-dialog
        title="修改静态属性"
        :visible.sync="editOnlyVisilbe"
        width="30%"
      >
        <el-form
          label-position="right"
          label-width="80px"
          :model="editOnlyForm"
        >
          <el-form-item label="新属性名">
            <el-input v-model="editOnlyForm.attr_name"></el-input>
          </el-form-item>
          <el-form-item label="属性值">
            <el-input v-model="editOnlyForm.attr_vals"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="editOnlyVisilbe = false">取 消</el-button>
          <el-button type="primary" @click="editOnlyInfo">确 定</el-button>
        </span>
      </el-dialog>

      <!-- 静态属性加载框 -->
      <el-dialog title="添加静态属性" :visible.sync="OnlyVisible" width="30%">
        <el-form label-position="left" label-width="80px" :model="onlyForm">
          <el-form-item label="属性名称">
            <el-input v-model="onlyForm.attr_name"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="OnlyVisible = false">取 消</el-button>
          <el-button type="primary" @click="addOnlyInfo">确 定</el-button>
        </span>
      </el-dialog>

      <!-- 标签页 -->
      <template>
        <el-tabs v-model="sel" @tab-click="handleClick">
          <el-tab-pane label="动态参数" name="many">
            <template>
              <el-button type="primary" @click="addMany">添加参数</el-button>
              <el-table :data="manyList" border style="width: 100%">
                <el-table-column type="expand">
                  <template slot-scope="scope">
                    <!-- {{ scope.row }} -->
                    <el-tag
                      :key="index"
                      v-for="(item, index) in scope.row.attr_vals"
                      closable
                      :disable-transitions="false"
                      @close="handleClose(scope.row,item)"
                    >
                      {{ item }}
                    </el-tag>
                    <el-input
                      class="input-new-tag"
                      v-if="inputVisible"
                      v-model="inputValue"
                      ref="saveTagInput"
                      size="small"
                      @keyup.enter.native="handleInputConfirm(scope.row)"
                      @blur="handleInputConfirm(scope.row)"
                    >
                    </el-input>
                    <el-button
                      v-else
                      class="button-new-tag"
                      size="small"
                      @click="showInput"
                      >+ New Tag</el-button
                    >
                  </template>
                </el-table-column>
                <el-table-column type="index" label="#"> </el-table-column>
                <el-table-column prop="attr_name" label="参数名称" width="180">
                </el-table-column>
                <el-table-column label="操作">
                  <template slot-scope="scope">
                    <el-button
                      type="primary"
                      icon="el-icon-edit"
                      size="mini"
                      @click="editMany(scope.row)"
                      >编辑</el-button
                    >

                    <el-button
                      type="danger"
                      icon="el-icon-delete"
                      size="mini"
                      @click="delMany(scope.row)"
                      >删除</el-button
                    >
                  </template>
                </el-table-column>
              </el-table>
            </template>
          </el-tab-pane>
          <el-tab-pane label="静态属性" name="only">
            <template>
              <el-button type="primary" @click="addOnly">添加属性</el-button>
              <el-table :data="onlyList" border style="width: 100%">
                <el-table-column prop="attr_name" label="参数名称" width="180">
                </el-table-column>
                <el-table-column label="操作">
                  <template slot-scope="scope">
                    <el-button
                      type="primary"
                      icon="el-icon-edit"
                      size="mini"
                      @click="editOnly(scope.row)"
                      >编辑</el-button
                    >

                    <el-button
                      type="danger"
                      icon="el-icon-delete"
                      size="mini"
                      @click="delOnly(scope.row)"
                      >删除</el-button
                    >
                  </template>
                </el-table-column>
              </el-table>
            </template>
          </el-tab-pane>
        </el-tabs>
      </template>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      editManyVisible: false,
      editOnlyVisilbe: false,
      inputVisible: false,
      inputValue: "",
      CateList: [],
      CateValue: [],
      manyList: [],
      onlyList: [],
      manyArray: [],
      editManyForm: {
        attr_name: "",
        attr_vals: "",
        id: "",
        attrid: "",
      },
      editOnlyForm: {
        attr_name: "",
        attr_vals: "",
        id: "",
        attrid: "",
      },
      ManyVisible: false,
      OnlyVisible: false,
      sel: "many",
      manyForm: {
        attr_name: "",
      },
      onlyForm: {
        attr_name: "",
      },
    };
  },
  mounted() {
    this.getshopCateList();
  },
  methods: {
    // 通过输入添加新标签属性
    async handleInputConfirm(row, item) {
      let inputValue = this.inputValue;
      if (inputValue) {
        row.attr_vals.push(inputValue);
        const {data:res} = await this.$axios.put('categories/'+this.CateValue[2]+'/attributes/'+row.attr_id,{
        attr_name:row.attr_name,
        attr_sel:row.attr_sel, 
        attr_vals:row.attr_vals.join(',')
      })
      if(res.meta.status == 200){
        this.$message.success("添加参数"+inputValue+"成功")
      }
      }
      this.inputVisible = false;
      this.inputValue = "";
    },

    // 添加新标签让Input聚焦
    showInput() {
      this.inputVisible = true;
      this.$nextTick((_) => {
        this.$refs.saveTagInput.$refs.input.focus();
      });
    },

    // 获取商品分类列表数据
    async getshopCateList() {
      const { data: res } = await this.$axios.get("categories");
      console.log(res);
      this.CateList = res.data;
    },

    // 点击删除动态属性标签
    async handleClose(row, item) {
      console.log(row);
      row.attr_vals.splice(row.attr_vals.indexOf(item), 1);
      const {data:res} = await this.$axios.put('categories/'+this.CateValue[2]+'/attributes/'+row.attr_id,{
        attr_name:row.attr_name,
        attr_sel:row.attr_sel,
        attr_vals:row.attr_vals.join(',')
      })
      console.log(res);
      if(res.meta.status == 200){
        this.$message.success("删除参数"+item+"成功")
      }
    },

    // 级联选择框改变触发
    async handleChange(val) {
      if (val.length > 2) {
        const { data: res } = await this.$axios.get(
          "categories/" + val[2] + "/attributes?sel=" + this.sel
        );
        if (this.sel == "many") {
          res.data.forEach((item) => {
            item.attr_vals = item.attr_vals.split(",");
          });
          this.manyList = res.data;
          console.log(this.manyList, 123);
        } else {
          this.onlyList = res.data;
        }
      } else {
        this.CateValue = [];
      }
    },

    // 点击修改动态参数
    editMany(row) {
      this.editManyForm.id = row.cat_id;
      this.editManyForm.attrid = row.attr_id;
      this.editManyForm.attr_name = row.attr_name;
      this.editManyForm.attr_vals = row.attr_vals;
      this.editManyVisible = true;
      console.log(this.editManyForm);
    },

    // 点击确定修改动态参数
    async editManyInfo() {
      // console.log("14234");
      const { data: res } = await this.$axios.put(
        "categories/" +
          this.editManyForm.id +
          "/attributes/" +
          this.editManyForm.attrid,
        {
          attr_name: this.editManyForm.attr_name,
          attr_vals: this.editManyForm.attr_vals,
          attr_sel: this.sel,
        }
      );
      // console.log(res);
      if (res.meta.status == 200) {
        this.$message.success("修改动态参数成功");
        const { data: res } = await this.$axios.get(
          "categories/" + this.CateValue[2] + "/attributes?sel=" + this.sel
        );
        if (this.sel == "many") {
          res.data.forEach((item) => {
            item.attr_vals = item.attr_vals.split(",");
          });
          this.manyList = res.data;
        } else {
          this.onlyList = res.data;
        }
        this.editManyVisible = false;
      }
    },

    // 点击确定修改静态属性
    async editOnlyInfo() {
      const { data: res } = await this.$axios.put(
        "categories/" +
          this.editOnlyForm.id +
          "/attributes/" +
          this.editOnlyForm.attrid,
        {
          attr_name: this.editOnlyForm.attr_name,
          attr_vals: this.editOnlyForm.attr_vals,
          attr_sel: this.sel,
        }
      );
      // console.log(res);
      if (res.meta.status == 200) {
        this.$message.success("修改静态属性成功");
        const { data: res } = await this.$axios.get(
          "categories/" + this.CateValue[2] + "/attributes?sel=" + this.sel
        );
        if (this.sel == "many") {
          res.data.forEach((item) => {
            item.attr_vals = item.attr_vals.split(",");
          });
          this.manyList = res.data;
        } else {
          this.onlyList = res.data;
        }
        this.editOnlyVisilbe = false;
      }
    },

    // 点击修改静态属性
    editOnly(row) {
      this.editOnlyForm.id = row.cat_id;
      this.editOnlyForm.attrid = row.attr_id;
      this.editOnlyForm.attr_name = row.attr_name;
      this.editOnlyForm.attr_vals = row.attr_vals;
      this.editOnlyVisilbe = true;
      console.log(this.editOnlyForm);
    },

    // 点击添加动态参数
    addMany() {
      if (this.CateValue.length > 0) {
        this.ManyVisible = true;
      }
    },
    //点击确定增加动态参数
    async addManyInfo() {
      const { data: res } = await this.$axios.post(
        "categories/" + this.CateValue[2] + "/attributes",
        {
          attr_sel: this.sel,
          attr_name: this.manyForm.attr_name,
        }
      );

      if (res.meta.status == 201) {
        this.$message.success("添加动态参数成功");
        this.manyForm.attr_name = "";
        const { data: res } = await this.$axios.get(
          "categories/" + this.CateValue[2] + "/attributes?sel=" + this.sel
        );
        if (this.sel == "many") {
          res.data.forEach((item) => {
            item.attr_vals = item.attr_vals.split(",");
          });
          this.manyList = res.data;
        } else {
          this.onlyList = res.data;
        }
        this.ManyVisible = false;
      }
    },

    // 点击添加静态属性
    addOnly() {
      if (this.CateValue.length > 0) {
        this.OnlyVisible = true;
      }
    },

    // 点击确定添加静态属性
    async addOnlyInfo() {
      const { data: res } = await this.$axios.post(
        "categories/" + this.CateValue[2] + "/attributes",
        {
          attr_sel: this.sel,
          attr_name: this.onlyForm.attr_name,
        }
      );

      if (res.meta.status == 201) {
        this.$message.success("添加静态属性成功");
        this.onlyForm.attr_name = "";
        const { data: res } = await this.$axios.get(
          "categories/" + this.CateValue[2] + "/attributes?sel=" + this.sel
        );
        if (this.sel == "many") {
          this.manyList = res.data;
        } else {
          this.onlyList = res.data;
        }
        this.OnlyVisible = false;
      }
    },

    //点击删除动态参数
    async delMany(row) {
      // console.log(row);
      const flag = await this.$confirm(
        "此操作将永久删除该参数, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      if (flag == "confirm") {
        const { data: res } = await this.$axios.delete(
          "categories/" + row.cat_id + "/attributes/" + row.attr_id
        );
        if (res.meta.status == 200) {
          this.$message.success("删除动态参数成功");
          const { data: res } = await this.$axios.get(
            "categories/" + this.CateValue[2] + "/attributes?sel=" + this.sel
          );
          if (this.sel == "many") {
            res.data.forEach((item) => {
              item = item.attr_vals.split(",");
            });
            this.manyList = res.data;
          } else {
            this.onlyList = res.data;
          }
        }
      }
    },

    // 点击删除静态参数
    async delOnly(row) {
      const flag = await this.$confirm(
        "此操作将永久删除该属性, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      if (flag == "confirm") {
        const { data: res } = await this.$axios.delete(
          "categories/" + row.cat_id + "/attributes/" + row.attr_id
        );
        if (res.meta.status == 200) {
          this.$message.success("删除动态参数成功");
          const { data: res } = await this.$axios.get(
            "categories/" + this.CateValue[2] + "/attributes?sel=" + this.sel
          );
          if (this.sel == "many") {
            this.manyList = res.data;
          } else {
            this.onlyList = res.data;
          }
        }
      }
    },

    // 切换标签页
    async handleClick(val) {
      if (this.CateValue.length > 2) {
        const { data: res } = await this.$axios.get(
          "categories/" + this.CateValue[2] + "/attributes?sel=" + this.sel
        );
        console.log(res);
        if (this.sel == "many") {
          this.manyList = res.data;
        } else {
          this.onlyList = res.data;
        }
      }
    },
  },
};
</script>

<style>
.elrow {
  margin-top: 20px;
}

.casSpan {
  line-height: 40px;
  height: 40px;
  text-align: center;
}

.el-tag + .el-tag {
  margin-left: 10px;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
</style>

<style module></style>
